package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.ContractRecordDto;
import com.zhangtai.modules.entity.contract.ContractRecordEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-10-20 17:57:00
 */
@Mapper
public interface ContractRecordDao extends BaseMapper<ContractRecordEntity> {

    @Update("update tb_contract_record set status = 2 where oid = #{oid} and kind = #{kind}")
    void removeRecord(@Param("oid") Long oid,@Param("kind") Integer kind);

    @Select("<script>SELECT t1.id, t4.code,t4.name,t4.gender,t4.idcard,t4.hiredate,t4.org_id,t4.unit_id,t4.job_id," +
            "t3.operate_type,t3.org_name begin_org_name,t3.unit_name begin_unit_name,t3.job_name begin_job_name," +
            "t3.contract_type begin_contract_type,t3.contract_begin_date,t3.contract_end_date,t3.employer_company," +
            "t3.contract_time_type,t3.contract_oss_url begin_contract_url,t2.contract_begin_date end_sign_date" +
            ",t4.employeestatus,t4.oid," +
            "t2.contract_oss_url end_contract_url,t1.status,t1.end_time,t1.end_type,t4.hiredate " +
            "FROM tb_contract_record t1 " +
            "LEFT JOIN tb_contract_manger t2 ON t1.end_contract_id = t2.id " +
            "LEFT JOIN tb_contract_manger t3 ON t3.id = t1.start_contract_id " +
            "LEFT JOIN tb_employee_info t4 on t4.oid = t1.oid" +
            "<where> t1.is_delete = 0 " +
            "<if test='code!=null and code !=&apos;&apos;'> and t4.code like '%${code}%'</if>" +
            "<if test='name!=null and name !=&apos;&apos;'> and t4.name like '%${name}%'</if>" +
            "<if test='contractType!=null'> and t3.contract_type = #{contractType}</if>" +
            "<if test='contractTimeType!=null'> and t3.contract_time_type = #{contractTimeType}</if>" +
            "<if test='status!=null'> and t1.status = #{status}</if>" +
            "<if test='contractEndDate!=null'> and t3.contract_end_date &lt;= '${contractEndDate} 23:59:59'</if>" +
            "<if test='orgId!=null and orgId.size>0'> and t4.org_id in " +
            "<foreach item=\"org\" index=\"index\" collection=\"orgId\" open=\"(\" separator=\",\" close=\")\">#{org}</foreach>" +
            "</if>"+
            "<if test='unitId!=null and unitId.size>0'> and t4.unit_id in " +
            "<foreach item=\"unit\" index=\"index\" collection=\"unitId\" open=\"(\" separator=\",\" close=\")\">#{unit}</foreach>" +
            "</if>"+
            "<if test='orgIds!=null and orgIds.size>0'> and t1.unit_id in " +
            "<foreach item=\"ids\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{ids}</foreach>" +
            "</if>" +
            "<if test='orgIds.size==0 or orgIds==null'> and t1.unit_id = null </if>"+
            "<if test='unitId !=null and unitId.size==0'> and t4.unit_id =null </if>" +
            "<if test='orgId !=null and orgId.size==0'> and t4.org_id =null </if>" +
            "</where>" +
            " order by t4.hiredate desc" +
            "</script>")
    List<ContractRecordDto> getRecordList(@Param("code")String code,
                                          @Param("name")String name,
                                          @Param("orgId")List<Long> orgId,
                                          @Param("unitId")List<Long> unitId,
                                          @Param("contractType")Integer contractType,
                                          @Param("contractTimeType")Integer contractTimeType,
                                          @Param("status")Integer status,
                                          @Param("contractEndDate")String contractEndDate,
                                          @Param("orgIds")List<Long> orgIds,
                                          IPage<ContractRecordDto> page);


    @Select("<script>SELECT t1.id, t4.code,t4.name,t4.gender,t4.idcard,t4.hiredate,t4.org_id,t4.unit_id,t4.job_id," +
            "t3.operate_type,t3.org_name begin_org_name,t3.unit_name begin_unit_name,t3.job_name begin_job_name," +
            "t3.contract_type begin_contract_type,t3.contract_begin_date,t3.contract_end_date,t3.employer_company," +
            "t3.contract_time_type,t3.contract_oss_url begin_contract_url,t2.contract_begin_date end_sign_date," +
            "t2.contract_oss_url end_contract_url,t1.status,t1.end_time,t1.end_type,t4.hiredate " +
            "FROM tb_contract_record t1 " +
            "LEFT JOIN tb_contract_manger t2 ON t1.end_contract_id = t2.id " +
            "LEFT JOIN tb_contract_manger t3 ON t3.id = t1.start_contract_id " +
            "LEFT JOIN tb_employee_info t4 on t4.oid = t1.oid" +
            "<where> t1.is_delete = 0 and t1.oid = #{oid}" +

            "</where>" +
            " order by t1.create_time desc" +
            "</script>")
    List<ContractRecordDto> getPersonRecordList(
                                            @Param("oid")Long oid,
                                          IPage<ContractRecordDto> page);

    @Select("<script>SELECT t1.id, t4.code,t4.name,t4.gender,t4.idcard,t4.hiredate,t4.org_id,t4.unit_id,t4.job_id," +
            "t3.operate_type,t3.org_name begin_org_name,t3.unit_name begin_unit_name,t3.job_name begin_job_name," +
            "t3.contract_type begin_contract_type,t3.contract_begin_date,t3.contract_end_date,t3.employer_company,t3.employer_company_id," +
            "t3.contract_time_type,t3.contract_oss_url begin_contract_url,t2.contract_begin_date end_sign_date," +
            "t2.contract_oss_url end_contract_url,t1.status,t1.end_time,t1.end_type,t4.hiredate " +
            "FROM tb_contract_record t1 " +
            "LEFT JOIN tb_contract_manger t2 ON t1.end_contract_id = t2.id " +
            "LEFT JOIN tb_contract_manger t3 ON t3.id = t1.start_contract_id " +
            "LEFT JOIN tb_employee_info t4 on t4.oid = t1.oid" +
            "<where> t1.is_delete = 0 and t1.oid = #{oid} and t1.status = 1 " +

            "</where>" +
            " order by t1.create_time desc" +
            "</script>")
    List<ContractRecordDto> getPersonRecordAllList(@Param("oid")Long oid);
}
